/*
 * This file is part of the eccOS Library.
 * Copyright (C) 2007.
 *
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 */
package org.openeccos.model;

import java.awt.event.ActionEvent;
import java.util.Calendar;
import java.util.Date;

import javax.persistence.Entity;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.swing.AbstractAction;

import org.hibernate.Session;

/**
 *
 * @author cgspinner@web.de
 */
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@Table(name = "t_eccos_task")
public class MTask extends MBase implements Comparable {

	public static final int PRIORITY_MIN = 1;
	public static final int PRIORITY_NORMAL = 3;
	public static final int PRIORITY_MAX = 5;

	public static final int STATUS_NEW = 0;
	public static final int STATUS_ACCEPTED = 1;
	public static final int STATUS_DONE_OK = 3;
	public static final int STATUS_DONE_REJECTED = 4;
	public static final int STATUS_ACCEPTED_AND_WAITING_FOR_ANSWER = 5;

	protected MRole assignedRole;
	protected MUser assignedUser;
	protected MUser createdBy;
	protected Date createdDate;
	protected String description;
	protected MUser doneBy;
	protected Date doneDate;
	protected boolean excludeCreator = false;
	protected boolean isShortcut;
	protected int modelId;
	protected String name;
	protected int priority = 3;
	protected int shortcutX;
	protected int shortcutY;
	protected int status;
	protected String actionClass;

	public MTask() {
		// Empty
	}

	public MTask(int modelId, String actionClass) {
		this(modelId, actionClass, null, null);
	}

	public MTask(int modelId, Class actionClass) {
		this(modelId, actionClass.getName(), null, null);
	}

	public MTask(int modelId, String actionClass, String name, String description) {
		this.modelId = modelId;
		this.actionClass = actionClass;
		this.name = name;
		this.description = description;
	}

	public int compareTo(Object other) {
		if (!(other instanceof MTask)) {
			return 0;
		}
		MTask otherTask = (MTask) other;
		return otherTask.priority - priority;
	}

	public void executeTask() {
		try {
			Class clazz = Class.forName(actionClass);
			AbstractAction action = (AbstractAction)clazz.newInstance();
			action.actionPerformed(new ActionEvent(this, modelId, ""));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@ManyToOne
	public MRole getAssignedRole() {
		return assignedRole;
	}

	@ManyToOne
	public MUser getAssignedUser() {
		return assignedUser;
	}

	@ManyToOne
	public MUser getCreatedBy() {
		return createdBy;
	}

	public Date getCreatedDate() {
		return createdDate;
	}

	public String getDescription() {
		return description;
	}

	@ManyToOne
	public MUser getDoneBy() {
		return doneBy;
	}

	public Date getDoneDate() {
		return doneDate;
	}

	public int getModelId() {
		return modelId;
	}

	public String getName() {
		return name;
	}

	public int getPriority() {
		return priority;
	}

	public int getShortcutX() {
		return shortcutX;
	}

	public int getShortcutY() {
		return shortcutY;
	}

	public int getStatus() {
		return status;
	}

	//override this method if needed
	public boolean isAssignedToUser(MUser user) {
		if (user == null) {
			return false;
		} else if (getDoneDate() != null) {
			return false;
		} else if (user.equals(getAssignedUser())) {
			return true;
		}
		return false;
	}

	public boolean isExcludeCreator() {
		return excludeCreator;
	}

	public boolean isShortcut() {
		return isShortcut;
	}

	public void setAssignedRole(MRole assignedRole) {
		this.assignedRole = assignedRole;
	}

	public void setAssignedUser(MUser assignedUser) {
		this.assignedUser = assignedUser;
	}

	public void setCreatedBy(MUser createdBy) {
		this.createdBy = createdBy;
	}

	public void setCreatedDate(Date createdDate) {
		this.createdDate = createdDate;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public void setDoneBy(MUser doneBy) {
		this.doneBy = doneBy;
	}

	public void setDoneDate(Date doneDate) {
		this.doneDate = doneDate;
	}

	public void setExcludeCreator(boolean excludeCreator) {
		this.excludeCreator = excludeCreator;
	}

	public void setModelId(int modelId) {
		this.modelId = modelId;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setPriority(int priority) {
		this.priority = priority;
	}

	public void setShortcut(boolean shortcut) {
		this.isShortcut = shortcut;
	}

	public void setShortcutX(int shortcutX) {
		this.shortcutX = shortcutX;
	}

	public void setShortcutY(int shortcutY) {
		this.shortcutY = shortcutY;
	}

	public void setStatus(int status) {
		this.status = status;
	}

	public String getActionClass() {
		return actionClass;
	}

	public void setActionClass(String actionClass) {
		this.actionClass = actionClass;
	}
}